//
//  LWProjReportZCtrl.m
//  laowu
//
//  Created by 尹桥印 on 15/5/1.
//  Copyright (c) 2015年 长沙市岳麓区立早网络科技有限公司. All rights reserved.
//  项目经理汇报页面

#import "LWProjReportZCtrl.h"
#import "CustomSpannerView.h"
#import "ProjectInfoResult.h"
#import "LWSelectProjCtrl.h"

@interface LWProjReportZCtrl ()<CustomSpannerViewDelegate,UITextViewDelegate,LWSelectProjCtrlDelegate>
@property (weak, nonatomic) IBOutlet UITextField *projNameField;
@property (nonatomic,weak)UIView *currentEditView;
@property (nonatomic,assign)CGFloat moveDis;//移动距离
@property (nonatomic,assign)CGFloat keyboardHeight;//键盘高度，如果为0，代表当前不是编辑模式
@property (nonatomic,strong)ProjectInfoResult *projectInfo;

@property (nonatomic,strong)WorkReportItemResult *workReportItem;
@end

@implementation LWProjReportZCtrl

-(instancetype)initWithWorkReportItemResult:(WorkReportItemResult *)item
{
    self = [super init];
    if (self) {
        self.workReportItem = item;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"今日汇报"];
    [self.enterBtn setBackgroundColor:THEME_COLOR];
    [self.enterBtn addTarget:self action:@selector(enterBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view1 setCornerRadius:0 borderWidth:1 borderColor:HEXColor(@"#dfdfdf")];
    [self.view2 setCornerRadius:0 borderWidth:1 borderColor:HEXColor(@"#dfdfdf")];
    [self.view3 setCornerRadius:0 borderWidth:1 borderColor:HEXColor(@"#dfdfdf")];
    [self.view4 setCornerRadius:0 borderWidth:1 borderColor:HEXColor(@"#dfdfdf")];
    
    [self.personCountTextView setDelegate:self];
    [self.jiaFStatusTextView setDelegate:self];
    [self.yiFStatusTextView setDelegate:self];
    [self.workDetailTextView setDelegate:self];
    
    
    //添加键盘弹出与隐藏的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (self.workReportItem) {
        [self.projNameField setText:self.workReportItem.project_name];
        [self.projStatusLabel setText:self.workReportItem.project_progress];
        [self.personCountTextView setText:self.workReportItem.work_num];
        [self.jiaFStatusTextView setText:self.workReportItem.first_part];
        [self.yiFStatusTextView setText:self.workReportItem.second_part];
        [self.workDetailTextView setText:self.workReportItem.work_place];
    }
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    NSLog(@"textViewShouldBeginEditing");
    self.currentEditView = textView;
    if (self.keyboardHeight!=0) {
        [self adjustLocationWithKeyboardHeight:self.keyboardHeight];
    }
    return YES;
}

- (void)keyboardWillShow:(NSNotification *)notif
{
    //在键盘显示的时候去调整view
    NSDictionary *info = [notif userInfo];
    NSValue *value = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGSize keyboardSize = [value CGRectValue].size;
    
    self.keyboardHeight = keyboardSize.height;
    [self adjustLocationWithKeyboardHeight:keyboardSize.height];
}

-(void)adjustLocationWithKeyboardHeight:(CGFloat)keyboardHeight{
    UIWindow * window=[[[UIApplication sharedApplication] delegate] window];
    CGRect editViewRect = [self.currentEditView convertRect:self.currentEditView.bounds toView:window];
    
    //首页判断键盘有没有遮挡view，如果没有遮挡，那个什么也不做
    CGFloat viewMaxY = CGRectGetMaxY(editViewRect);
    if (viewMaxY+10>SCREENH-keyboardHeight) {
        //代表键盘遮挡了view
        //需要调整uiscrollview的contentoffset移动
        self.moveDis = (viewMaxY+10)-(SCREENH-keyboardHeight);
        [UIView animateWithDuration:0.25 animations:^{
            [self.scrollView setContentOffset:CGPointMake(0, self.scrollView.contentOffset.y+self.moveDis)];
        }];
    }
}

-(void)keyboardWillHide:(NSNotification*)notif{
    self.keyboardHeight = 0;
    self.currentEditView = nil;
    if (self.scrollView.contentOffset.y!=0) {
        [self.scrollView setContentOffset:CGPointMake(0, self.scrollView.contentOffset.y-self.moveDis)];
    }
    self.moveDis = 0;
}

- (IBAction)statusBtnClick:(id)sender {
    UIButton *btn = sender;
    NSArray *stringArray = STRING_ARRAY_PROJECT_STATUS;
    if (btn.tag==1) {
        //上班情况
        stringArray = STRING_ARRAY_PROJECT_STATUS;
    }
    CustomSpannerView *spannerView = [[CustomSpannerView alloc] initWithStringArray:stringArray bearingView:btn];
    [spannerView setDelegate:self];
    [spannerView show];
}

-(void)customSpannerItemClick:(NSString *)title index:(NSInteger)index bearingView:(UIView *)bearingView{
    if (bearingView.tag == 1) {
        [self.projStatusLabel setText:title];
    }
}

-(void)enterBtnClick:(UIButton *)btn{
    if (ISBLANK(self.projNameField.text) || ISBLANK(self.personCountTextView.text) || ISBLANK(self.jiaFStatusTextView.text) || ISBLANK(self.yiFStatusTextView.text) || ISBLANK(self.workDetailTextView.text)) {
        [DialogUtils showTipsWithView:nil String:@"请将数据填写完整"];
        return;
    }
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setObject:APP_KEY_VALUE forKey:APP_KEY_KEY];
    
    if (self.workReportItem) {
        [param setObject:[NSNumber numberWithInteger:self.workReportItem.id] forKey:@"id"];
        [param setObject:[NSNumber numberWithInteger:self.workReportItem.content_id] forKey:@"content_id"];
    }else{
        [param setObject:[NSNumber numberWithInteger:self.projectInfo.id] forKey:@"content_id"];
    }
    
    
    [param setObject:[NSNumber numberWithInteger:[UserDefaultsHelper integerForKey:KEY_USER_ID]] forKey:@"user_id"];
    
    [param setObject:self.jiaFStatusTextView.text forKey:@"first_part"];
    [param setObject:self.yiFStatusTextView.text forKey:@"second_part"];
    [param setObject:self.personCountTextView.text forKey:@"worker_num"];
    [param setObject:self.workDetailTextView.text forKey:@"work_place"];
    
    NSString *url = self.workReportItem?@"api/workreport_update.json":@"/api/workreport.json";
    [MeDataTool workReportWithParam:param url:url success:^(BaseResponse *result) {
        if (result.isSucc) {
            [DialogUtils showTipsWithView:nil String:@"提交成功"];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [DialogUtils showTipsWithView:nil String:result.msg];
        }
    } failure:^(NSError *error) {
        [DialogUtils showTipsWithView:nil String:@"提交失败"];
    }];
}

-(void)dealloc{
    //移除键盘监听
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
- (IBAction)tap:(id)sender {
    [self.view endEditing:YES];
}
- (IBAction)selectProjBtnClick:(id)sender {
    //去选择项目
    LWSelectProjCtrl *ctrl = [[LWSelectProjCtrl alloc] init];
    ctrl.delegate = self;
    [self.navigationController pushViewController:ctrl animated:YES];
}

-(void)lwSelectProjectItemClick:(ProjectInfoResult *)info{
    self.projectInfo = info;
    [self.projNameField setText:info.name];
}
@end
